package com.iseastar.guojiang.newcabinet;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.AppLogic;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.NewParcelBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.iseastar.guojiang.model.TaskResultPriceBean;
import com.iseastar.guojiang.model.TaskStageBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.StringUtils;

import java.util.ArrayList;

import droid.frame.utils.Android;

import static droid.frame.App.getHandler;

/**
 * Created by joker on 2017/12/28.
 * 同城任务阶段
 */
public class SameCityTaskStageFragment extends BaseFragment implements View.OnClickListener {

    private TextView mSurplusTimeTV;//剩余时间
    private TextView mAddressNameTV;//地址名称
    private TextView mAddressDetailsTV;//详细地址
    private TextView mRemarkTV;//备注
    private TextView mNameTV;//寄件人或收件人姓名
    private ImageView mTaskStepIconIV;//任务阶段步骤图标
    /**
     * 顶部步骤
     */
    private ImageView mStepImageView1;
    private TextView mStepTextView1;
    private ImageView mStepImageView2;
    private TextView mStepTextView2;
    /**
     * 第一步相关布局
     */
    private LinearLayout mFirstStepLL;//第一步的布局
    private ImageView mFirstStepImageIV;//第一步步骤图片展示
    private TextView mFirstStepTitleTV;//第一步的名称
    private TextView mFirstStepTipTV;//第一步提示内容
    private TextView mFirstStepRightTV;//第一步右边按钮操作
    /**
     * 第二步相关布局
     */
    private LinearLayout mSecondStepLL;//第二步的布局
    private ImageView mSecondStepImageIV;//第二步步骤的图片展示
    private TextView mSecondStepTitleTV;//第二步的名称
    private TextView mSecondStepTipTV;//第二步提示
    private TextView mSecondStepRightTV1;//第二步右边按钮操作
    private TextView mSecondStepRightTV2;//第二步右边按钮操作
    /**
     * 第三步相关布局
     */
    private LinearLayout mThirdStepLL;//第三步的布局
    private ImageView mThirdStepImageIV;//第三步步骤的图片展示
    private TextView mThirdStepTitleTV;//第三步的名称
    private TextView mThirdStepTipTV;//第三步提示
    private TextView mThirdStepRightTV1;//第三步右边按钮操作
    /**
     * 第四步相关布局
     */
    private LinearLayout mFourthStepLL;//第四步的布局
    private ImageView mFourthStepImageIV;//第四步步骤的图片展示
    private TextView mFourthStepTitleTV;//第四步的名称
    private TextView mFourthStepTipTV;//第四步提示
    private LinearLayout mQrcodeSendcodeLL;//第四步右边按钮布局
    private TextView mFourthStepRightTV1;//第四步右边按钮操作
    private TextView mFourthStepRightTV2;//第四步右边按钮操作
    private TextView mAffirmArrivedTV;//第四步右边按钮操作

    private ServiceTaskBean serviceTaskBean = null;
    private CountDownTimer timer = null;
    private TaskStageBean stageBean = null;
    private boolean isCallPhone = false;//是否上门拨打联系人电话
    private int recLen = 45;
    private boolean isStart;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null != bundle) {
            serviceTaskBean = (ServiceTaskBean) bundle.getSerializable("item");
            showLoadingDialog(null);
            AppHttp.getInstance().getTaskStageDetail(serviceTaskBean.getTaskId());
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.fragment_samecity_task_stage);
        super.findViewById();
        mSurplusTimeTV = (TextView) findViewById(R.id.surplus_time_tv);
        mAddressNameTV = (TextView) findViewById(R.id.address_name_tv);
        mAddressDetailsTV = (TextView) findViewById(R.id.address_details_tv);
        mRemarkTV = (TextView) findViewById(R.id.remark_tv);
        mNameTV = (TextView) findViewById(R.id.name_tv);
        findViewById(R.id.phone_tv).setOnClickListener(this);
        mTaskStepIconIV = (ImageView) findViewById(R.id.task_step_icon_iv);
        findViewById(R.id.map_tv).setOnClickListener(this);
        findViewById(R.id.navigation_tv).setOnClickListener(this);
        mStepImageView1 = (ImageView) findViewById(R.id.step_image_iv);
        mStepTextView1 = (TextView) findViewById(R.id.step_tv);
        mStepImageView2 = (ImageView) findViewById(R.id.step_image2_iv);
        mStepTextView2 = (TextView) findViewById(R.id.step2_tv);
        //******************************** 第一步布局*******************************************//
        mFirstStepLL = (LinearLayout) findViewById(R.id.step_first_ll);
        mFirstStepImageIV = (ImageView) findViewById(R.id.first_step_image_iv);
        mFirstStepTitleTV = (TextView) findViewById(R.id.first_title_tv);
        mFirstStepTipTV = (TextView) findViewById(R.id.first_step_tip_tv);
        mFirstStepRightTV = (TextView) findViewById(R.id.first_step_right_tv);
        mFirstStepRightTV.setOnClickListener(this);
        //********************************第二步布局*******************************************/
        mSecondStepLL = (LinearLayout) findViewById(R.id.step_second_ll);
        mSecondStepImageIV = (ImageView) findViewById(R.id.second_step_image_iv);
        mSecondStepTitleTV = (TextView) findViewById(R.id.second_step_title_tv);
        mSecondStepTipTV = (TextView) findViewById(R.id.second_step_tip_tv);
        mSecondStepRightTV1 = (TextView) findViewById(R.id.scan_qrcode_tv);
        mSecondStepRightTV1.setOnClickListener(this);//扫描二维码
        mSecondStepRightTV2 = (TextView) findViewById(R.id.confirm_send_code_tv);
        mSecondStepRightTV2.setOnClickListener(this);//验证寄件码
        //******************************* 第三步布局 **********************************************/
        mThirdStepLL = (LinearLayout) findViewById(R.id.step_third_ll);
        mThirdStepImageIV = (ImageView) findViewById(R.id.third_step_image_iv);
        mThirdStepTitleTV = (TextView) findViewById(R.id.third_step_title_tv);
        mThirdStepTipTV = (TextView) findViewById(R.id.third_step_tip_tv);
        mThirdStepRightTV1 = (TextView) findViewById(R.id.third_step_right_tv);
        mThirdStepRightTV1.setOnClickListener(this);//联系收件人
        //********************************第四步布局*******************************************/
        mFourthStepLL = (LinearLayout) findViewById(R.id.step_fourth_ll);
        mFourthStepImageIV = (ImageView) findViewById(R.id.fourth_step_image_iv);
        mFourthStepTitleTV = (TextView) findViewById(R.id.fourth_step_title_tv);
        mFourthStepTipTV = (TextView) findViewById(R.id.fourth_step_tip_tv);
        mQrcodeSendcodeLL = (LinearLayout) findViewById(R.id.qrcode_sendcode_ll);
        mFourthStepRightTV1 = (TextView) findViewById(R.id.fourth_resend_code_tv);
        mFourthStepRightTV1.setOnClickListener(this);//扫描二维码
        mFourthStepRightTV2 = (TextView) findViewById(R.id.fourth_confirm_send_code_tv);
        mFourthStepRightTV2.setOnClickListener(this);//验证寄件码
        mAffirmArrivedTV = (TextView) findViewById(R.id.affirm_arrived_tv);
        mAffirmArrivedTV.setOnClickListener(this);//确认已送达
        updateView();
    }

    /**
     * 更新数据
     */
    public void updateView() {
        if (serviceTaskBean != null) {
            if (isEmpty(serviceTaskBean.getRemark())) {
                mRemarkTV.setVisibility(View.GONE);
            } else {
                mRemarkTV.setVisibility(View.VISIBLE);
                mRemarkTV.setText("备注：" + serviceTaskBean.getRemark());
            }
            long overTime = serviceTaskBean.getOverTime();
            long startTime = serviceTaskBean.getCreateTime();
            if (System.currentTimeMillis() > overTime) {
                mSurplusTimeTV.setText("已超时");
            } else {
                remaintimeCountdown(mSurplusTimeTV, overTime - startTime);
                timer = new CountDownTimer(overTime - startTime, 60 * 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        remaintimeCountdown(mSurplusTimeTV, millisUntilFinished);
                    }

                    @Override
                    public void onFinish() {
                        mSurplusTimeTV.setText("已超时");
                    }
                };
                timer.start();
            }
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            //跳转到地图
            case R.id.map_tv:
                if (serviceTaskBean != null && serviceTaskBean.getLatitude() > 0 && serviceTaskBean.getLongitude() > 0 && stageBean != null) {
                    intent = new Intent(getActivityContext(), BaiDuMapActivity.class);
                    //寄件
                    if (Const.CourierTaskFinishStatus.contractSender == stageBean.getStepType() || Const.CourierTaskFinishStatus.CityAcceptanceSendCode == stageBean.getStepType()) {
                        intent.putExtra("lat", serviceTaskBean.getLatitude());
                        intent.putExtra("lon", serviceTaskBean.getLongitude());
                        intent.putExtra("gdLat", serviceTaskBean.getGdLatitude());
                        intent.putExtra("gdLon", serviceTaskBean.getGdLongitude());
                        intent.putExtra("type", 0);
                    } else {
                        intent.putExtra("lat", serviceTaskBean.getGiveLatitude());
                        intent.putExtra("lon", serviceTaskBean.getGiveLongitude());
                        intent.putExtra("gdLat", serviceTaskBean.getGiveGdLatitude());
                        intent.putExtra("gdLon", serviceTaskBean.getGiveGdLongitude());
                        intent.putExtra("type", 1);
                    }
                    intent.putExtra("taskType", serviceTaskBean.getTaskType());
                    startActivity(intent);
                }
                break;
            //跳转到地图导航
            case R.id.navigation_tv:
                if (serviceTaskBean != null && serviceTaskBean.getLatitude() > 0 && serviceTaskBean.getLongitude() > 0) {
                    //寄件
                    if (Const.CourierTaskFinishStatus.contractSender == stageBean.getStepType() || Const.CourierTaskFinishStatus.CityAcceptanceSendCode == stageBean.getStepType()) {
                        DialogMgr.showSelectMapToNavigation(getActivityContext(), findViewById(R.id.navigation_tv), serviceTaskBean.getLatitude(), serviceTaskBean.getLongitude(), serviceTaskBean.getGdLatitude(), serviceTaskBean.getGdLongitude());
                    } else {
                        DialogMgr.showSelectMapToNavigation(getActivityContext(), findViewById(R.id.navigation_tv), serviceTaskBean.getGiveLatitude(), serviceTaskBean.getGiveLongitude(), serviceTaskBean.getGiveGdLatitude(), serviceTaskBean.getGiveGdLongitude());
                    }
                }
                break;
            //拨打寄件人电话
            case R.id.first_step_right_tv:
                if (serviceTaskBean != null) {
                    isCallPhone = true;
                    showLoadingDialog(null);
                    AppHttp.getInstance().updateTaskStepContractData(serviceTaskBean.getTaskId());
                }
                break;
            //拨打寄件人或收件人
            case R.id.phone_tv:
                if (serviceTaskBean != null && stageBean != null) {
                    isCallPhone = true;
                    showLoadingDialog(null);
                    //拨打寄件人
                    if (Const.CourierTaskFinishStatus.contractSender == stageBean.getStepType() || Const.CourierTaskFinishStatus.CityAcceptanceSendCode == stageBean.getStepType()) {
                        AppHttp.getInstance().updateTaskStepContractData(serviceTaskBean.getTaskId());
                    } else {//拨打收件人
                        AppHttp.getInstance().finishReceiverContractData(serviceTaskBean.getTaskId());
                    }
                }
                break;
            //验收包裹，扫描二维码
            case R.id.scan_qrcode_tv:
                intent = new Intent(getActivityContext(), TaskCaptureActivity.class);
                intent.setAction(Intents.courier_scan_for_task);
                startActivityForResult(intent, Const.RequestCode.courier_scan_task);
                break;
            //验证寄件码
            case R.id.confirm_send_code_tv:
//                intent = new Intent(getActivityContext(), ConfirmSendCodeDialogActivity.class);
//                intent.putExtra("type", 0);
//                intent.putExtra("item", serviceTaskBean);
//                startActivityForResult(intent, 1001);
                DialogMgr.confirmSendCodeDialog(getActivityContext(),serviceTaskBean,0);
                break;
            //联系收件人电话
            case R.id.third_step_right_tv:
                if (serviceTaskBean != null) {
                    isCallPhone = true;
                    showLoadingDialog(null);
                    AppHttp.getInstance().finishReceiverContractData(serviceTaskBean.getTaskId());
                }
                break;
            //验证收件码
            case R.id.fourth_confirm_send_code_tv:
//                intent = new Intent(getActivityContext(), ConfirmSendCodeDialogActivity.class);
//                intent.putExtra("type", 1);
//                intent.putExtra("item", serviceTaskBean);
//                startActivityForResult(intent, 1002);
                DialogMgr.confirmSendCodeDialog(getActivityContext(),serviceTaskBean,1);
                break;
            //重新发送收件码
            case R.id.fourth_resend_code_tv:
                if (serviceTaskBean != null) {
                    showLoadingDialog(null);
                    AppHttp.getInstance().sameCityGetReceiveCode(serviceTaskBean.getTaskId());
                }
                break;
            //在不需要收件码的情况下确认已送达
            case R.id.affirm_arrived_tv:
                DialogMgr.affirmArrived(this);
                break;

        }
    }

    private void countDownStart() {
        getHandler().postDelayed(runnable, 1000);
        isStart = true;
        mFourthStepRightTV1.setEnabled(false);
    }

    private Runnable runnable = new Runnable() {
        public void run() {
            if (recLen == 1) {
                isStart = false;
                recLen = 45;
                mFourthStepRightTV1.setEnabled(true);
                mFourthStepRightTV1.getPaint().setFlags(0);
                mFourthStepRightTV1.setText("重新发送收件码");
            }

            if (recLen == 44) {
                showToast("收件码已发送");
            }
            if (isStart) {
                recLen--;
                mFourthStepRightTV1.setFocusable(false);
                mFourthStepRightTV1.getPaint().setFlags(0);
                mFourthStepRightTV1.setText(recLen + "秒后重新发送");
                getHandler().postDelayed(this, 1000);
            }
        }
    };

    /**
     * 根据步骤展示相应的界面
     *
     * @param bean
     */
    private void updateViewForStep(TaskStageBean bean) {
        if (Const.CourierTaskFinishStatus.contractSender == bean.getStepType() || Const.CourierTaskFinishStatus.CityAcceptanceSendCode == bean.getStepType()) {
            mAddressNameTV.setText(serviceTaskBean.getBuildName());
            mAddressDetailsTV.setText(serviceTaskBean.getAddress());
            mTaskStepIconIV.setImageResource(R.drawable.samecity_take_icon);
            mNameTV.setText("寄件人：" + serviceTaskBean.getContactName());
        } else {
            mAddressNameTV.setText(serviceTaskBean.getGiveBuildName());
            mAddressDetailsTV.setText(serviceTaskBean.getGiveAddress());
            mTaskStepIconIV.setImageResource(R.drawable.samecity_send_icon);
            mNameTV.setText("收件人：" + serviceTaskBean.getGiveContactName());
        }
        if (Const.CourierTaskFinishStatus.contractSender == bean.getStepType()) {//第一步进行中
            mStepImageView1.setImageResource(R.drawable.samecity_yellow_icon);
            mStepTextView1.setTextColor(Color.parseColor("#FFCC00"));
            mStepImageView2.setImageResource(R.drawable.samecity_gray_icon);
            mStepTextView2.setTextColor(Color.parseColor("#C8C8C8"));
            //第一步
            mFirstStepLL.setVisibility(View.VISIBLE);
            mFirstStepLL.setBackgroundColor(Color.parseColor("#FFE226"));
            mFirstStepImageIV.setImageResource(R.drawable.samecity_step_first_icon);
            mFirstStepTitleTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mFirstStepTipTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mFirstStepRightTV.setBackgroundResource(R.drawable.samecity_contract_sneder_bg);
            //第二步
            mSecondStepLL.setVisibility(View.VISIBLE);
            mSecondStepLL.setBackgroundColor(getResources().getColor(R.color.white));
            mSecondStepImageIV.setImageResource(R.drawable.task_second_step_gray_icon);
            mSecondStepTitleTV.setTextColor(Color.parseColor("#D7D7D7"));
            mSecondStepTipTV.setTextColor(Color.parseColor("#D7D7D7"));
            mSecondStepRightTV1.setBackgroundResource(R.drawable.task_contact_sender_phone_d7d7d7_bg);
            mSecondStepRightTV1.setTextColor(Color.parseColor("#d7d7d7"));
            mSecondStepRightTV1.setEnabled(false);
            mSecondStepRightTV2.setBackgroundResource(R.drawable.task_contact_sender_phone_d7d7d7_bg);
            mSecondStepRightTV2.setTextColor(Color.parseColor("#d7d7d7"));
            mSecondStepRightTV2.setEnabled(false);
            //第三步
            mThirdStepLL.setVisibility(View.GONE);
            //第四步
            mFourthStepLL.setVisibility(View.GONE);
        } else if (Const.CourierTaskFinishStatus.CityAcceptanceSendCode == bean.getStepType()) {//第二步进行中
            mStepImageView1.setImageResource(R.drawable.samecity_yellow_icon);
            mStepTextView1.setTextColor(Color.parseColor("#FFCC00"));
            mStepImageView2.setImageResource(R.drawable.samecity_gray_icon);
            mStepTextView2.setTextColor(Color.parseColor("#C8C8C8"));
            //第一步
            mFirstStepLL.setVisibility(View.VISIBLE);
            mFirstStepLL.setBackgroundColor(getResources().getColor(R.color.white));
            mFirstStepImageIV.setImageResource(R.drawable.task_finish_green_icon);
            mFirstStepTitleTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mFirstStepTipTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mFirstStepRightTV.setBackgroundResource(R.drawable.samecity_contract_sneder_bg);
            //第二步
            mSecondStepLL.setVisibility(View.VISIBLE);
            mSecondStepLL.setBackgroundColor(Color.parseColor("#FFE226"));
            mSecondStepImageIV.setImageResource(R.drawable.samecity_step_second_icon);
            mSecondStepTitleTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mSecondStepTipTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mSecondStepRightTV1.setBackgroundResource(R.drawable.samecity_contract_sneder_bg);
            mSecondStepRightTV1.setTextColor(getResources().getColor(R.color.app_font_1));
            mSecondStepRightTV1.setEnabled(true);
            mSecondStepRightTV2.setBackgroundResource(R.drawable.samecity_contract_sneder_bg);
            mSecondStepRightTV2.setTextColor(getResources().getColor(R.color.app_font_1));
            mSecondStepRightTV2.setEnabled(true);
            //第三步
            mThirdStepLL.setVisibility(View.GONE);
            //第四步
            mFourthStepLL.setVisibility(View.GONE);
        } else if (Const.CourierTaskFinishStatus.ContactReceiver == bean.getStepType()) {//第三步进行中
            mStepImageView1.setImageResource(R.drawable.samecity_yellow_finish_icon);
            mStepTextView1.setTextColor(Color.parseColor("#FFCC00"));
            mStepImageView2.setImageResource(R.drawable.samecity_gray_icon);
            mStepTextView2.setTextColor(Color.parseColor("#C8C8C8"));
            //第一步
            mFirstStepLL.setVisibility(View.GONE);
            //第二步
            mSecondStepLL.setVisibility(View.GONE);
            //第三步
            mThirdStepLL.setVisibility(View.VISIBLE);
            mThirdStepLL.setBackgroundColor(Color.parseColor("#FFE226"));
            mThirdStepImageIV.setImageResource(R.drawable.samecity_step_first_icon);
            mThirdStepTitleTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mThirdStepTipTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mThirdStepRightTV1.setBackgroundResource(R.drawable.samecity_contract_sneder_bg);
            //第四步
            if (serviceTaskBean.getReceiveCode()) {
                mQrcodeSendcodeLL.setVisibility(View.VISIBLE);
                mFourthStepRightTV1.setEnabled(false);
                mFourthStepRightTV2.setEnabled(false);
                mAffirmArrivedTV.setVisibility(View.GONE);
            } else {
                mQrcodeSendcodeLL.setVisibility(View.GONE);
                mAffirmArrivedTV.setVisibility(View.VISIBLE);
                mAffirmArrivedTV.setEnabled(false);
            }
            mFourthStepLL.setVisibility(View.VISIBLE);
            mFourthStepLL.setBackgroundColor(getResources().getColor(R.color.white));
            mFourthStepImageIV.setImageResource(R.drawable.task_second_step_gray_icon);
            mFourthStepTitleTV.setTextColor(Color.parseColor("#D7D7D7"));
            mFourthStepTipTV.setTextColor(Color.parseColor("#D7D7D7"));
            mFourthStepRightTV1.setBackgroundResource(R.drawable.task_contact_sender_phone_d7d7d7_bg);
            mFourthStepRightTV1.setTextColor(Color.parseColor("#d7d7d7"));
            mFourthStepRightTV2.setBackgroundResource(R.drawable.task_contact_sender_phone_d7d7d7_bg);
            mFourthStepRightTV2.setTextColor(Color.parseColor("#d7d7d7"));
        } else if (Const.CourierTaskFinishStatus.CityAcceptanceReceiverCode == bean.getStepType()) {//第四步进行中
            mStepImageView1.setImageResource(R.drawable.samecity_yellow_finish_icon);
            mStepTextView1.setTextColor(Color.parseColor("#FFCC00"));
            mStepImageView2.setImageResource(R.drawable.samecity_yellow_icon);
            mStepTextView2.setTextColor(Color.parseColor("#FFCC00"));
            //第一步
            mFirstStepLL.setVisibility(View.GONE);
            //第二步
            mSecondStepLL.setVisibility(View.GONE);
            //第三步
            mThirdStepLL.setVisibility(View.VISIBLE);
            mThirdStepLL.setBackgroundColor(getResources().getColor(R.color.white));
            mThirdStepImageIV.setImageResource(R.drawable.task_finish_green_icon);
            mThirdStepTitleTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mThirdStepTipTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mThirdStepRightTV1.setBackgroundResource(R.drawable.samecity_contract_sneder_bg);
            mAffirmArrivedTV.setEnabled(false);
            //第四步
            if (serviceTaskBean.getReceiveCode()) {
                mQrcodeSendcodeLL.setVisibility(View.VISIBLE);
                mFourthStepRightTV1.setEnabled(true);
                mFourthStepRightTV2.setEnabled(true);
                mAffirmArrivedTV.setVisibility(View.GONE);
            } else {
                mFourthStepTipTV.setText("请收件人签收后再点击确认已送达，提前点击会有相应惩罚");
                mQrcodeSendcodeLL.setVisibility(View.GONE);
                mAffirmArrivedTV.setVisibility(View.VISIBLE);
                mAffirmArrivedTV.setEnabled(true);
                mAffirmArrivedTV.setTextColor(getResources().getColor(R.color.app_font_1));
                mAffirmArrivedTV.setBackgroundResource(R.drawable.samecity_contract_sneder_bg);
            }
            mFourthStepLL.setVisibility(View.VISIBLE);
            mFourthStepLL.setBackgroundColor(Color.parseColor("#FFE226"));
            mFourthStepImageIV.setImageResource(R.drawable.samecity_step_second_icon);
            mFourthStepTitleTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mFourthStepTipTV.setTextColor(getResources().getColor(R.color.app_font_1));
            mFourthStepRightTV1.setBackgroundResource(R.drawable.samecity_contract_sneder_bg);
            mFourthStepRightTV1.setTextColor(getResources().getColor(R.color.app_font_1));
            mFourthStepRightTV2.setBackgroundResource(R.drawable.samecity_contract_sneder_bg);
            mFourthStepRightTV2.setTextColor(getResources().getColor(R.color.app_font_1));

        }
    }

    /**
     * 确认已送达
     */
    public void affirmArrivedData() {
        AppHttp.getInstance().confirmReceiverCodeData(null, serviceTaskBean.getTaskId());
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //获取当前任务阶段操作步骤数据返回
            case MsgID.courier_task_step_detail:
                cancelLoadingDialog();
                final ReqResult<TaskStageBean> reqResult = JSON.parser(msg.obj, TaskStageBean.class);
                if (checkLoginStatus(reqResult)) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            stageBean = reqResult.getResult();
                            if (stageBean == null) {
                                return;
                            }
                            updateViewForStep(stageBean);
                        }
                    });
                } else {
                    isCallPhone = false;
                    showToast(reqResult.getMessage());
                }
                return false;
            //当前任务阶段操作步骤进行更新返回
            case MsgID.courier_task_step_update:
            case MsgID.courier_task_step_receiver_contract://完成收件人联系
                cancelLoadingDialog();
                final ReqResult<TaskStageBean> result = JSON.parser(msg.obj, TaskStageBean.class);
                if (checkLoginStatus(result)) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            stageBean = result.getResult();
                            if (stageBean == null) {
                                return;
                            }
                            if (Const.CourierTaskFinishStatus.CityAcceptanceSendCode == stageBean.getStepType()) {
                                if (isCallPhone) {
                                    DialogMgr.receiverOrSendCall(getActivity(), serviceTaskBean.getPhone(), 1,serviceTaskBean.getAddress(),null);
                                }
                            }
                            if (Const.CourierTaskFinishStatus.CityAcceptanceReceiverCode == stageBean.getStepType()) {
                                if (isCallPhone) {
                                    DialogMgr.receiverOrSendCall(getActivity(), serviceTaskBean.getGivePhone(), 2,serviceTaskBean.getGiveAddress(),serviceTaskBean.getContactName());
                                }
                            }
                            isCallPhone = false;
                            updateViewForStep(stageBean);
                        }
                    });
                } else {
                    isCallPhone = false;
                    showToast(result.getMessage());
                }
                return true;
            //验收收件码后，看看是否验收成功
            case MsgID.courier_check_receiver_code_data:
                ReqResult<TaskResultPriceBean> receiverResult = JSON.parser(msg.obj, TaskResultPriceBean.class);
                if (checkLoginStatus(receiverResult)) {
                    TaskResultPriceBean result1 = receiverResult.getResult();
                    if (result1 != null) {
                        Intent intent = new Intent(getActivityContext(), SameCityCompleteTheTaskActivity.class);
                        intent.putExtra("item", result1);
                        startActivity(intent);
                        getActivityContext().finish();
                    }
                } else {
                    showToast(receiverResult.getMessage());
                }
                return true;
            //返回包裹列表数据
            case MsgID.courier_scan_user_qrcode_data://扫描用户或驿站二维码
            case MsgID.courier_check_code_data://寄件码验证
                cancelLoadingDialog();
                final ReqResult<NewParcelBean> parcelResult = JSON.parseList(msg.obj, NewParcelBean.class);
                if (checkLoginStatus(parcelResult)) {
                    ArrayList<NewParcelBean> parcelList = parcelResult.getResultList();
                    if (parcelList != null) {
                        Intent intent = new Intent(getActivityContext(), ParcelListActivity.class);
                        intent.putExtra("items", parcelList);
                        startActivity(intent);
                    }
                } else {
                    showToast(parcelResult.getMessage());
                }
                return true;
            //重新获取收件码
            case MsgID.courier_samecity_getreceive_code:
                cancelLoadingDialog();
                ReqResult<?> getReceiveCodeResult = JSON.parser(msg.obj);
                if (checkLoginStatus(getReceiveCodeResult)) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast("发送成功");
                            countDownStart();
                        }
                    });
                } else {
                    showToast(getReceiveCodeResult.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    private void remaintimeCountdown(TextView countDown, long remainTime) {
        String[] diffs = AppLogic.timeDiffStr(remainTime);
        String timeText = "";
        if (diffs.length == 0) {
            timeText = "00小时00分";
        } else if (diffs.length == 1) {
            timeText = "00小时00分";
        } else if (diffs.length == 2) {
            timeText = "00小时" + diffs[0] + "分";
        } else if (diffs.length == 3) {
            timeText = diffs[0] + "小时" + diffs[1] + "分";
        }
        int sencondStart = timeText.indexOf(":");
        SpannableString secondSp = new SpannableString(timeText);
        secondSp.setSpan(new TextAppearanceSpan(getActivityContext(), R.style.task_count_down_style1), sencondStart + 1, timeText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        countDown.setText("剩余时间：" + secondSp, TextView.BufferType.SPANNABLE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //扫描二维码返回的数据
        if (resultCode == Activity.RESULT_OK && Const.RequestCode.courier_scan_task == requestCode) {
            String result = data.getStringExtra("result");
            if (StringUtils.isEmpty(result)) {
                return;
            }
            int index = result.lastIndexOf("=");
            String result2;
            if (index > -1) {
                result2 = result.substring(index + 1, result.length());
            } else {
                result2 = result;
            }
            showLoadingDialog(null);
            AppHttp.getInstance().scanUserQrCodeData(result2, serviceTaskBean.getTaskId());
        }
        //验证寄件码返回
        else if (resultCode == Activity.RESULT_OK && 1001 == requestCode) {
            Android.hideSoftInput(getActivityContext());
            String code = data.getStringExtra("code");
            showLoadingDialog(null);
            AppHttp.getInstance().confirmSendCodeData(code, serviceTaskBean.getTaskId());
        }
        //验证收件码返回
        else if (resultCode == Activity.RESULT_OK && 1002 == requestCode) {
            Android.hideSoftInput(getActivityContext());
            String code = data.getStringExtra("code");
            AppHttp.getInstance().confirmReceiverCodeData(code, serviceTaskBean.getTaskId());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    /**
     * 获取验证码
     */
    public void setSendCode(int type,String code){
        showLoadingDialog(null);
        if (type==1){
            AppHttp.getInstance().confirmReceiverCodeData(code, serviceTaskBean.getTaskId());
        }else {
            AppHttp.getInstance().confirmSendCodeData(code, serviceTaskBean.getTaskId());
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
        getHandler().removeCallbacks(runnable);
    }
}
